package cn.edu.scau.dormitory_management_system.service.impl;

import cn.edu.scau.dormitory_management_system.dao.DormDao;
import cn.edu.scau.dormitory_management_system.dao.DormbuildDao;
import cn.edu.scau.dormitory_management_system.entity.DormEntity;
import cn.edu.scau.dormitory_management_system.entity.DormbuildEntity;
import cn.edu.scau.dormitory_management_system.entity.SysLogEntity;
import cn.edu.scau.dormitory_management_system.entity.UserEntity;
import cn.edu.scau.dormitory_management_system.entity.dto.LogDTO;
import cn.edu.scau.dormitory_management_system.entity.dto.StuDTO;
import cn.edu.scau.dormitory_management_system.service.BedService;
import cn.edu.scau.dormitory_management_system.service.DormService;
import cn.edu.scau.dormitory_management_system.service.DormbuildService;
import cn.edu.scau.dormitory_management_system.service.SysLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;



@Service("dormbuildService")
public class DormbuildServiceImpl extends ServiceImpl<DormbuildDao, DormbuildEntity> implements DormbuildService {

    @Autowired
    private DormDao dormDao;

    @Autowired
    private  DormbuildDao dormbuildDao;

    @Autowired
    private BedService bedService;

    @Autowired
    private DormService dormService;

    @Override
    public  List<DormbuildEntity> getAllBuild(){
        List<DormbuildEntity> dormbuildEntities = this.baseMapper.selectList(null);
        return dormbuildEntities;
    }

    @Override
    public boolean create(DormbuildEntity dormbuildEntity){
        boolean flag = this.baseMapper.insert(dormbuildEntity) > 0;
        if(!flag) return flag;
        for(int floor=1;floor<=dormbuildEntity.getFloor();floor++){
            for(int room=1;room<=dormbuildEntity.getRoomsPerfloor();room++){
                String dormName =dormbuildEntity.getDormBuildName()+"#"+String.valueOf(floor)+String.format("%02d",room);
                DormEntity dormEntity = new DormEntity();
                dormEntity.setDormName(dormName);
                dormEntity.setBuildId(dormbuildEntity.getBuildId());
                dormEntity.setSex(1);
                dormEntity.setDepartment("");
                dormEntity.setStuNum(0);
                flag = dormDao.insert(dormEntity) > 0;
                if(!flag) return flag;
                flag = bedService.insertBed(dormEntity.getDormId(),dormbuildEntity.getCapacity()) > 0;
                if(!flag) return flag;
            }
        }
        return  flag;
    }

    @Autowired
    private SysLogService logService;
    @Autowired
    private LogDTO logDTO;

    private void addLog(long ms, int sum) {
        SysLogEntity logEntity = new SysLogEntity();
        logEntity.setIpAddr(logDTO.getIp());
        logEntity.setCity(logDTO.getCity());
        logEntity.setUserName(logDTO.getUserName());
        logEntity.setExeTime(ms);
        String op = "";
        if(sum > 1) op = "批量";
        logEntity.setOperation("在表tb_dorm中" + op + "插入" + sum + "条记录");
        logEntity.setOpTime(new Date());
        logService.save(logEntity);
    }

    @Override
    public void createList(List<DormbuildEntity> dormbuildEntities){
        List<DormEntity> dormEntities = new ArrayList<>();
        List<Integer> ids = new ArrayList<>();
        List<Integer> n = new ArrayList<>();
        for (DormbuildEntity dormbuildEntity:dormbuildEntities) {
            for (int floor = 1; floor <= dormbuildEntity.getFloor(); floor++) {
                for (int room = 1; room <= dormbuildEntity.getRoomsPerfloor(); room++) {
                    String dormName = dormbuildEntity.getDormBuildName() + "#" + String.valueOf(floor) + String.format("%02d", room);
                    DormEntity dormEntity = new DormEntity();
                    dormEntity.setDormName(dormName);
                    dormEntity.setBuildId(dormbuildEntity.getBuildId());
                    dormEntity.setSex(1);
                    dormEntity.setDepartment("");
                    dormEntity.setStuNum(0);
                    dormEntities.add(dormEntity);
                    n.add(dormbuildEntity.getCapacity());
                }
            }
        }

        int count = dormService.max();

        Date date = new Date();
        dormService.saveBatch(dormEntities);
        addLog(new Date().getTime() - date.getTime(), dormEntities.size());

        for (int i = 1; i <= dormEntities.size(); i++) {
            ids.add(count + i);
        }
        bedService.insertList(ids, n);
    }

    @Override
    public boolean delete(DormbuildEntity dormbuildEntity){
        boolean flag = this.baseMapper.deleteById(dormbuildEntity.getBuildId()) > 0;
        if(!flag) return flag;
        flag = dormService.deleteDormInBuild(dormbuildEntity.getBuildId());
        return flag;
    }

    @Override
    public boolean update(DormbuildEntity dormbuildEntity) {
        return  this.baseMapper.updateById(dormbuildEntity) > 0;
    }


    @Override
    public List<DormbuildEntity> findBuildByName(String buildName){
        List<DormbuildEntity> dormbuildEntities = this.baseMapper.selectList(new QueryWrapper<DormbuildEntity>().like("dorm_build_name", buildName));
        return  dormbuildEntities;
    }

    @Override
    public IPage<StuDTO> getStuDTO(IPage<StuDTO> page, int buildId) {
        return dormbuildDao.getStuDTO(page,buildId);
    }


}
